#include "manager.h"

manager::manager(){
	flag = INIT;
	frontConn = NULL;
	backConn = NULL;
	
}
manager::~manager(){

}

int manager::createConn(){

	frontConn = new front();
	if( frontConn == NULL ){
		return -1;
	}
	frontConn->setManager(this);
	
	backConn = new back();
	if( backConn == NULL ){
		return -1;
	}
	backConn->setManager(this);
	return 0;
	
}

int manager::setFrontFd(int fd){
	if( frontConn == NULL ){
		return -1;
	}
	frontConn->setFd(fd);
	flag = PROXY;
	return 0;
}

int manager::sendToBack(const char*msg, int msg_len){
	return backConn->send(msg, msg_len);;
}

int manager::sendToFront(const char*msg, int msg_len){
	return frontConn->send(msg, msg_len);
}

int manager::setBackIpPort(const string &ip, int port){
	if( backConn == NULL ){
		return -1;
	}
	if( port < 0 ){
		return -2;
	}
	backConn->setAddrInfo(ip, port);
	return 0;
}


int manager::closeConn(){
	if( frontConn != NULL ){
		delete frontConn;
		frontConn = NULL;
	}
	if( backConn != NULL ){
		delete backConn;
		backConn = NULL;
	}
	delete this;
	return 0;

}


